class AccountController < ApplicationController

  def login
    if request.post?
      @current_user = User.find_by_login_and_password(
      params[:login], params[:password])
      unless @current_user.nil?
        session[:user_id] = @current_user.id
        unless session[:return_to].blank?
          redirect_to session[:return_to]
          session[:return_to] = nil
        else
          redirect_to :controller => 'story'
        end
      else
        flash[:warning] = 'Wrong user name or password'
      end
    end
  end

  def logout
    session[:user_id] = @current_user = nil
  end
  
  def show
    @user = User.find_by_login(params[:id])
    unless @user.nil?
      @stories_submitted = Story.find_all_by_user_id(@user)
      @stories_voted_on = @user.stories_voted_on
      @stories_commented_on = @user.stories_commented_on
    end
  end
  
  def register
    @user = User.new(params[:user])
    if request.post? and @user.save
       flash[:notice] = 'Registration succeeded'
      redirect_to :action => 'login'
    end
  end
end